package com.zenithmind.version.controller;

import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.version.mapper.RepositoryMapper.RepositoryStatistics;
import com.zenithmind.version.pojo.dto.RepositoryCreateDTO;
import com.zenithmind.version.pojo.vo.RepositoryVO;
import com.zenithmind.version.service.RepositoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "仓库管理", description = "仓库相关接口")
@RestController
@RequestMapping("/api/v1/repositories")
@RequiredArgsConstructor
public class RepositoryController {

    private final RepositoryService repositoryService;

    @Operation(summary = "创建仓库", description = "创建新的代码仓库")
    @PostMapping
    public Result<RepositoryVO> createRepository(@Valid @RequestBody RepositoryCreateDTO createDTO) {
        RepositoryVO repository = repositoryService.createRepository(createDTO);
        return Result.success(repository);
    }

    @Operation(summary = "分页查询仓库列表", description = "分页查询仓库列表")
    @GetMapping
    public Result<PageResult<RepositoryVO>> getRepositoryPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "所有者ID") @RequestParam(required = false) String ownerId,
            @Parameter(description = "仓库类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "仓库状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        PageResult<RepositoryVO> result = repositoryService.getRepositoryPage(current, size, ownerId, type, status, keyword);
        return Result.success(result);
    }

    @Operation(summary = "查询仓库详情", description = "根据ID查询仓库详情")
    @GetMapping("/{id}")
    public Result<RepositoryVO> getRepositoryById(@Parameter(description = "仓库ID") @PathVariable String id) {
        RepositoryVO repository = repositoryService.getRepositoryById(id);
        return Result.success(repository);
    }

    @Operation(summary = "根据名称和所有者查询仓库", description = "根据仓库名称和所有者查询仓库")
    @GetMapping("/{ownerName}/{repositoryName}")
    public Result<RepositoryVO> getRepositoryByNameAndOwner(
            @Parameter(description = "所有者名称") @PathVariable String ownerName,
            @Parameter(description = "仓库名称") @PathVariable String repositoryName) {
        // 这里需要根据所有者名称查询所有者ID，为了简化直接使用名称
        RepositoryVO repository = repositoryService.getRepositoryByNameAndOwner(repositoryName, ownerName);
        return Result.success(repository);
    }

    @Operation(summary = "更新仓库信息", description = "更新仓库基本信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateRepository(
            @Parameter(description = "仓库ID") @PathVariable String id,
            @Valid @RequestBody RepositoryCreateDTO updateDTO) {
        Boolean result = repositoryService.updateRepository(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除仓库", description = "删除指定仓库")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteRepository(@Parameter(description = "仓库ID") @PathVariable String id) {
        Boolean result = repositoryService.deleteRepository(id);
        return Result.success(result);
    }

    @Operation(summary = "归档仓库", description = "归档指定仓库")
    @PostMapping("/{id}/archive")
    public Result<Boolean> archiveRepository(@Parameter(description = "仓库ID") @PathVariable String id) {
        Boolean result = repositoryService.archiveRepository(id);
        return Result.success(result);
    }

    @Operation(summary = "取消归档仓库", description = "取消归档指定仓库")
    @PostMapping("/{id}/unarchive")
    public Result<Boolean> unarchiveRepository(@Parameter(description = "仓库ID") @PathVariable String id) {
        Boolean result = repositoryService.unarchiveRepository(id);
        return Result.success(result);
    }

    @Operation(summary = "Fork仓库", description = "Fork指定仓库")
    @PostMapping("/{id}/fork")
    public Result<RepositoryVO> forkRepository(
            @Parameter(description = "源仓库ID") @PathVariable String id,
            @Parameter(description = "新仓库名称") @RequestParam String name) {
        RepositoryVO repository = repositoryService.forkRepository(id, name);
        return Result.success(repository);
    }

    @Operation(summary = "查询用户仓库列表", description = "查询指定用户的仓库列表")
    @GetMapping("/user/{userId}")
    public Result<List<RepositoryVO>> getUserRepositories(
            @Parameter(description = "用户ID") @PathVariable String userId,
            @Parameter(description = "仓库类型") @RequestParam(required = false) Integer type) {
        List<RepositoryVO> repositories = repositoryService.getUserRepositories(userId, type);
        return Result.success(repositories);
    }

    @Operation(summary = "查询公开仓库列表", description = "查询公开仓库列表")
    @GetMapping("/public")
    public Result<List<RepositoryVO>> getPublicRepositories(
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "编程语言") @RequestParam(required = false) String language,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<RepositoryVO> repositories = repositoryService.getPublicRepositories(keyword, language, limit);
        return Result.success(repositories);
    }

    @Operation(summary = "检查仓库名称可用性", description = "检查仓库名称是否可用")
    @GetMapping("/check-name")
    public Result<Boolean> checkRepositoryNameAvailable(
            @Parameter(description = "仓库名称") @RequestParam String name,
            @Parameter(description = "所有者ID") @RequestParam String ownerId) {
        Boolean available = repositoryService.checkRepositoryNameAvailable(name, ownerId);
        return Result.success(available);
    }

    @Operation(summary = "获取克隆URL", description = "获取仓库的克隆URL")
    @GetMapping("/{id}/clone-url")
    public Result<String> getCloneUrl(
            @Parameter(description = "仓库ID") @PathVariable String id,
            @Parameter(description = "协议类型") @RequestParam(defaultValue = "http") String protocol) {
        String cloneUrl = repositoryService.getCloneUrl(id, protocol);
        return Result.success(cloneUrl);
    }

    @Operation(summary = "更新仓库统计信息", description = "更新仓库的统计信息")
    @PostMapping("/{id}/update-statistics")
    public Result<Boolean> updateRepositoryStatistics(@Parameter(description = "仓库ID") @PathVariable String id) {
        Boolean result = repositoryService.updateRepositoryStatistics(id);
        return Result.success(result);
    }

    @Operation(summary = "获取仓库统计信息", description = "获取用户的仓库统计信息")
    @GetMapping("/statistics")
    public Result<RepositoryStatistics> getRepositoryStatistics(
            @Parameter(description = "所有者ID") @RequestParam String ownerId) {
        RepositoryStatistics statistics = repositoryService.getRepositoryStatistics(ownerId);
        return Result.success(statistics);
    }
}
